package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.pro.ProcessStep;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.ProcessStepMapper;
import com.qidianit.mes.pro.service.ProcessService;
import com.qidianit.mes.pro.service.ProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/17 0017 - 16:10
 */
@Service
public class ProcessStepServiceImpl implements ProcessStepService {

    @Autowired
    private ProcessStepMapper processStepMapper;

    @Override
    public Result selectAll(Long processId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<ProcessStep> processSteps = processStepMapper.selectAll(processId);
        return Result.success(new PageInfo<>(processSteps));
    }
    @Override
    public Result<ProcessStep> selectById(Long id) {
        return Result.success(processStepMapper.selectById(id));
    }

    @Override
    public Result insertProcessStep(ProcessStep processStep) {
        if (processStep.getProcessId() == null ||
                processStep.getStepOrder() == null ||
                processStep.getMaterialUrl() == null || "".equals( processStep.getMaterialUrl()) ||
                processStep.getAuxiliaryEquipment() == null || "".equals( processStep.getAuxiliaryEquipment()) ||
                processStep.getAuxiliaryMaterials() == null || "".equals( processStep.getAuxiliaryMaterials()) ||
                processStep.getStepDescription() == null || "".equals( processStep.getStepDescription()) ||
                processStep.getRemarks() == null || "".equals( processStep.getRemarks())
        ){
            return Result.error("添加失败，输入信息不完整");
        }
        ArrayList<ProcessStep> processSteps = processStepMapper.selectAll(processStep.getProcessId());
        for (ProcessStep step : processSteps) {
            if(Objects.equals(step.getStepOrder(), processStep.getStepOrder())){
                return Result.error("添加失败，步骤有重复");
            }
        }
        return processStepMapper.insertProcessStep(processStep)==1?Result.success(processStep):Result.error("操作失败");
    }

    @Override
    public Result updateById(ProcessStep processStep) {
        if (processStep.getStepOrder() == null ||
                processStep.getMaterialUrl() == null || "".equals(processStep.getMaterialUrl()) ||
                processStep.getAuxiliaryEquipment() == null || "".equals(processStep.getAuxiliaryEquipment()) ||
                processStep.getAuxiliaryMaterials() == null || "".equals(processStep.getAuxiliaryMaterials()) ||
                processStep.getStepDescription() == null || "".equals(processStep.getStepDescription()) ||
                processStep.getRemarks() == null || "".equals(processStep.getRemarks())
        ){
            return Result.error("修改失败，修改信息不完整");
        }
        ArrayList<ProcessStep> processSteps = processStepMapper.selectAll(processStep.getProcessId());
        for (ProcessStep step : processSteps) {
            if(Objects.equals(step.getStepOrder(), processStep.getStepOrder()) && step.getId() != processStep.getId()){
                return Result.error("添加失败，步骤有重复");
            }
        }
        return processStepMapper.updateById(processStep)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return processStepMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }
}
